package Trie;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class window extends JFrame {
    JPanel jPanel;
    JLabel jLabelChinese,jLabelEnglish,jLabel;
    //JTextField jTextEnglish,jTextChinese;
    JTextArea jTextEnglish,jTextChinese,jTextArea;
    JButton jButtonTranslate,jButtonClean,jButtonInsert,jButtonDelete;
    Box boxLabel,boxButton1,boxButton2,boxText,boxMain;
    Trie trie;

    window(Trie tr){
        trie = new Trie();
        setLayout(new FlowLayout());
        Init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void Init(){

        setTitle("英汉字典");
        setBounds(100,200,400,350);
        jLabelEnglish = new JLabel("英文单词");
        jLabelChinese = new JLabel("英文翻译");
        jLabel = new JLabel("你可能要查找的单词");
        jButtonClean = new JButton("清除");
        jButtonInsert = new JButton("添加");
        jButtonDelete = new JButton("删除");
        jButtonTranslate = new JButton("翻译");
        jTextChinese = new JTextArea(1,10);
        jTextEnglish = new JTextArea(1,10);

        jTextArea = new JTextArea(8,20);

        jButtonTranslate.addActionListener((ActionEvent e) -> {
            try {
                String s = jTextChinese.toString();
                LinkedList<TrieNode> list;
                list = trie.search(s);
                for (TrieNode trieNode : list) {
                    System.out.println(trieNode.key);
                }
                int i = 0;
                System.out.println(1);
                if(list != null){
                    if(list.get(i).key.equals(s)){
                        System.out.println(1);
                        jTextChinese.setText(list.get(i++).value);
                    }else{
                        for(;i < list.size(); ++i){
                            jTextArea.setText(list.get(i).key + "\n");
                            jTextArea.setText(list.get(i).value + "\n");
                        }
                    }
                }else{

                }
            }catch (Exception exception){
                exception.printStackTrace();
            }
        });

        boxButton1 = Box.createHorizontalBox();
        boxButton2 = Box.createHorizontalBox();
        boxLabel = Box.createHorizontalBox();
        boxText = Box.createHorizontalBox();
        boxMain = Box.createVerticalBox();

        boxButton1.add(jButtonTranslate);
        boxButton1.add(Box.createHorizontalStrut(50));
        boxButton1.add(jButtonClean);

        boxButton2.add(jButtonInsert);
        boxButton2.add(Box.createHorizontalStrut(50));
        boxButton2.add(jButtonDelete);

        boxLabel.add(jLabelEnglish);
        boxLabel.add(Box.createHorizontalStrut(30));
        boxLabel.add(jLabelChinese);

        boxText.add(jTextEnglish);
        boxText.add(Box.createHorizontalStrut(30));
        boxText.add(jTextChinese);

        boxMain.add(boxLabel);
        boxMain.add(Box.createVerticalStrut(20));
        boxMain.add(boxText);
        boxMain.add(Box.createVerticalStrut(10));
        boxMain.add(jLabel);
        boxMain.add(jTextArea);
        boxMain.add(Box.createVerticalStrut(20));
        boxMain.add(boxButton1);
        boxMain.add(Box.createVerticalStrut(20));
        boxMain.add(boxButton2);

        jPanel = new JPanel();
        jPanel.add(boxMain);
        add(jPanel);
    }
}

public class Dictionarie {
    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("apple","苹果");
        trie.insert("applepp","苹果屁屁");
        trie.insert("app","app");
        trie.insert("appleap","苹果a屁");
        trie.insert("applebp","苹果b屁");
        trie.insert("applecp","苹果c屁");
        trie.insert("appledp","苹果d屁");
        trie.insert("appleep","苹果e屁");
        trie.insert("banana","香蕉");
        trie.insert("cola","可乐");
        //trie.search("colo");
        window w = new window(trie);

    }
}
